#include "Sprite.hpp"
#include "Shader.hpp"

namespace phx
{

Sprite::Sprite(Texture &texture) : 
	mTextureUnit(texture.mTextureUnit),
	mPos(),
	mSize(texture.mWidth, texture.mHeight)
{

}

void Sprite::setPosition(phxFloat x, phxFloat y)
{
	mPos.x = x;
	mPos.y = y;
}

void Sprite::setPosition(const vec2f &pos)
{
	mPos = pos;
}

void Sprite::setSize(phxFloat width, phxFloat height)
{
	mSize.x = width;
	mSize.y = height;
}

void Sprite::setSize(const vec2f &size)
{
	mSize = size;
}

void Sprite::render()
{
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDepthMask(0); // Disable writing to depth buffer

	DefaultShader::sprite.use();
	DefaultShader::sprite.setValue("spriteTexture", mTextureUnit);
	DefaultShader::sprite.setValue("spritePosition", vec2f((mPos.x / 1280.0f) * 2.0f, (mPos.y / 768.0f) * 2.0f));
	DefaultShader::sprite.setValue("spriteSize", vec2f(mSize.x / 1280.0f, mSize.y / 768.0f));
	//setPosition(mPos);
	//setSize(mSize);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	glDisable(GL_BLEND);
	glDepthMask(1); // Enable writing to depth buffer
}

}